package in.muton.edushare.poll.components;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.poll.pages.PollResults;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.Poll;

public class ResultLink extends Link<IModel<Poll>> {

	private static final long serialVersionUID = 1294779778029896514L;

	public ResultLink(String id, IModel<Poll> model) {
		super(id);
		pollModel = model;
	}

	@Override
	public void onClick() {
		PageParameters p = new PageParameters();
		p.add("0", Integer.toString(pollModel.getObject().getPollId()));
		setResponsePage(PollResults.class, p);
	}

	@Override
	public boolean isVisible() {
		UserPollDAO upDao = new UserPollDAO(EduShareSession.get()
				.getSessionORMInterface());
		boolean isOpenPoll = pollModel.getObject().getPollResultsPublic() == Poll.POLL_RESULT_OPEN;
		boolean isReviewer = upDao.getPollObservers(pollModel.getObject())
				.contains(EduShareSession.get().getLoggedInUser());
		boolean isOwner = pollModel.getObject().getCreator() == EduShareSession
				.get().getLoggedInUser();
		return isOpenPoll || isReviewer || isOwner;
	}

	IModel<Poll> pollModel = null;
}
